package com.cms.action;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.action.DefaultAction;
import com.cms.model.Channel;
import com.cms.model.Project;
import com.cms.service.ChannelService;
import com.cms.service.ChannelServiceImpl;
import com.cms.service.ProjectService;
import com.cms.service.ProjectServiceImpl;
import com.util.Util;

public class ProjectAction extends DefaultAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Logger logger;
	private ProjectService service  = new ProjectServiceImpl();
	private ChannelService cservice  = new ChannelServiceImpl();
	private String        type;
	private List<Project> list;
	private Project model;
	private Channel channel;
	private String msg;
	private String txt_keyword;
	
	public ProjectAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	public String list()
	{
		channel = cservice.getChannel(channel.getId());
		list = service.getProjects(channel.getId());
		return SUCCESS;
	}
	
	public String show()
	{
		if(msg.equals("update"))
		{
			channel = cservice.getChannel(channel.getId());
			model = service.getProject(model.getId());
		}
		else
		{
			model = new Project();
			model.setId(-1);
			model.setShipDate(Util.dateToString(new Date(),"yyyy-MM-dd"));
			model.setChannelId(channel.getId());
		}

		return SUCCESS;
	}
	
	public String save()
	{
		if(msg.equals("insert"))
		{
			if(service.insertProject(model))
			{
				msg="项目管理：添加项目成功！！";
			}
			else
			{
				msg="项目管理：对不起，添加项目可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("update"))
		{
			if(service.updateProject(model))
			{
				msg="项目管理：更新项目成功！！";
			}
			else
			{
				msg="项目管理：对不起，更新项目可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("del"))
		{
			if(service.deleteProject(model))
			{
				msg="项目管理：删除项目成功！！";
			}
			else
			{
				msg="项目管理：对不起，删除项目可能存在问题，请稍候重试一下！！";
			}
		}
		return SUCCESS;
	}
	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTxt_keyword() {
		return txt_keyword;
	}

	public void setTxt_keyword(String txtKeyword) {
		txt_keyword = txtKeyword;
	}

	public List<Project> getList() {
		return list;
	}

	public void setList(List<Project> list) {
		this.list = list;
	}

	public Project getModel() {
		return model;
	}

	public void setModel(Project model) {
		this.model = model;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}
	
}
